package com.songcha.module_login.ui.activity.login

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_business.bean.user.UserInfoBean
import com.songcha.library_business.proxy.login.LoginEventProxy
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_common.helper.SharedPreferenceHelper
import com.songcha.library_network.bean.BaseBean
import com.songcha.library_network.bean.BaseStringDataBean
import io.reactivex.Observable

class LoginViewModel(app: Application):BaseViewModel<LoginRepository>(app) {
    enum class LoginState{
        SUCCESS,
        ERROR
    }
    //59-0
    val verifyCountDown=MutableLiveData(0)

    val loginState=MutableLiveData<LoginState>()

    val wxLoginState=MutableLiveData<LoginState>()

    val sendVerifySuccess=MutableLiveData<Boolean>()

    @JvmField
    var isCheckPrivacyPolicy=false

    init {
        isCheckPrivacyPolicy=getCheckPrivacyPolicy()
    }

    fun setCheckPrivacyPolicy(isCheck:Boolean){
        SharedPreferenceHelper.setBoolean("login","checkPrivacyPolicy",isCheck)
    }

    fun getCheckPrivacyPolicy():Boolean{
        return SharedPreferenceHelper.getBoolean("login","checkPrivacyPolicy")
    }

    fun updateClientIp(){
        handleApiDataObserver(repository!!.updateClientIp(),object:IApiResult<BaseBean>{
            override fun onSuccess(bean: BaseBean) {

            }

        },false,false,false)
    }

    fun login(phone:String,code:String){
        val obs=repository!!.login(phone,code)
            .flatMap(object :io.reactivex.functions.Function<BaseStringDataBean,Observable<UserInfoBean>>{
                override fun apply(t: BaseStringDataBean): Observable<UserInfoBean> {
                    UserInfoManager.saveLogin(t.data)
                    updateClientIp()
                    return repository!!.getUserInfo()
                }
            })

        handleApiDataObserver(obs,object : IApiResult<UserInfoBean>{
            override fun onSuccess(bean: UserInfoBean) {
                loginState.value=LoginState.SUCCESS
                UserInfoManager.saveUserInfo(bean.data)
                LoginEventProxy.postLoginSuccessEvent()
            }

            override fun onError(e: Throwable) {
                loginState.value=LoginState.ERROR
                UserInfoManager.clearLogin()
            }

        })

    }

    fun wxlogin(code:String){
        val obs=repository!!.wxlogin(code)
            .flatMap(object :io.reactivex.functions.Function<BaseStringDataBean,Observable<UserInfoBean>>{
                override fun apply(t: BaseStringDataBean): Observable<UserInfoBean> {
                    UserInfoManager.saveLogin(t.data)
                    return repository!!.getUserInfo()
                }
            })

        handleApiDataObserver(obs,object : IApiResult<UserInfoBean>{
            override fun onSuccess(bean: UserInfoBean) {
                wxLoginState.value=LoginState.SUCCESS
                UserInfoManager.saveUserInfo(bean.data)
                LoginEventProxy.postLoginSuccessEvent()
            }

            override fun onError(e: Throwable) {
                wxLoginState.value=LoginState.ERROR
                UserInfoManager.clearLogin()
            }

        })
    }

    fun sendVerify(phone:String){
       // sendVerifySuccess.value=false
        handleApiDataObserver(repository!!.sendVerifyCode(phone),object : IApiResult<BaseBean>{
            override fun onSuccess(bean: BaseBean) {
                //verifyCountDown.value=0
                sendVerifySuccess.value=true
            }

            override fun onError(e: Throwable) {
                //verifyCountDown.value=0
                verifyCountDown.value=0
                sendVerifySuccess.value=false

            }

        })
    }

    fun setCountDown(countDown:Int){
        verifyCountDown.value=countDown
    }

}